package com.lhs.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lhs.mapper.Vip_RecordMapper;
import com.lhs.Result.Result;
import com.lhs.service.Vip_RecordService;
import com.lhs.VO.user.vipVO;
import com.lhs.entity.user.Vip_record;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.time.LocalDateTime;

@Service
public class Vip_RecordServiceimpl extends ServiceImpl<Vip_RecordMapper, Vip_record> implements Vip_RecordService {


    @Autowired
    private Vip_RecordMapper vip_recordMapper;

    /*
    查询VIP
     * @param userId
     * @return vipVO
     */
    @Override
    public Result<vipVO> getVipRecord(Long userId) {
        // 参数校验
        if (userId == null) {
            return Result.error("用户ID不能为空");
        }

        Vip_record vip = vip_recordMapper.selectByUserId(userId);

        // 数据库查询结果空值检查
        if (vip == null) {
            return Result.success(new vipVO());
        }

        vipVO vipVO = new vipVO();
        vipVO.setVipLevel(vip.getVipLevel());

        // 查询是否到期 - 修正逻辑：当前时间在结束时间之后表示已到期
        if (vip.getEndTime() != null && vip.getEndTime().isBefore(LocalDateTime.now())) {
            vipVO.setIsVip(false); // 已到期，不是VIP
            long daysExpired = java.time.Duration.between(vip.getEndTime(), LocalDateTime.now()).toDays();
            vipVO.setVipExpire("会员已过期" + daysExpired + "天");
        } else {
            vipVO.setIsVip(true); // 未到期，是VIP
            vipVO.setVipExpire(vip.getEndTime());
        }
        return Result.success(vipVO);
    }
}
